﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Client {
	static class Program {
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(String[] args) {
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(true);

			Logger.Log("Client app has started");

			Form form;

			if (args.Length > 0) {
				Logger.Log("Command Line Arguments detected, parsing...");
				String arg;

				String host = "localhost", iface = null;
				Decimal port = 8000;
				Boolean connect = false;
				Boolean autoreconnect = true;

				for (int i = 0; i < args.Length; ++i) {
					arg = args[i];
					if (arg.StartsWith("-") && arg.Length == 2) {
						if (arg[1].Equals('h')) {
							++i;
							if (i < args.Length) {
								host = args[i];
								Logger.Debug("CLI parser# Host found: '" + host + "'");
							}
						} else if (arg[1].Equals('i')) {
							++i;
							if (i < args.Length) {
								iface = args[i];
								Logger.Debug("CLI parser# Interface found: '" + iface + "'");
							}
						} else if (arg[1].Equals('p')) {
							++i;
							if (i < args.Length) {
								port = Decimal.Parse(args[i]);
								Logger.Debug("CLI parser# Port found: '" + port + "'");
							}
						} else if (arg[1].Equals('c')) {
							connect = true;
							Logger.Debug("CLI parser# Connect immediately");
						} else if (arg[1].Equals('a')) {
							autoreconnect = true;
							Logger.Debug("CLI parser# Auto reconnect switched on");
						} else if (arg[1].Equals('1')) {
							autoreconnect = false;
							Logger.Debug("CLI parser# Auto reconnect switched off");
						} else {
							Logger.Warning("CLI parser# Invalid argument type '" + arg[1] + "'");
						}
					} else {
						Logger.Warning("CLI parser# Invalid argument '" + arg + "'");
					}
				}

				form = new Form1(host, port, iface, connect, autoreconnect);
			} else {
				form = new Form1();
			}

			Application.Run(form);
		}
	}
}
